package com.yto.hrreport.event.buttons
{
	import com.yto.hrreport.command.ReportCommand;
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.TextInput;
	import mx.events.CloseEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class DeleteButtonEvent  extends ButtonEvent {
		
		private static var deleteEvent : DeleteButtonEvent = new DeleteButtonEvent();
		
		public var mouseEvent : MouseEvent;
		
		private var param : Object;
		
		private static var parentName : String;
		
		public function DeleteButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : DeleteButtonEvent {
			DeleteButtonEvent.parentName = parentName;
			return deleteEvent;
		}
		
		public function deleteHandler(event : CloseEvent) : void {
			
			param = ReportCommand.getInstance(parentName).initParam("onBoDelete");
			
			if(event.detail == Alert.OK) {
				var tableServer : RemoteObject = new RemoteObject();
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.deleteStatisticData(param.headGrid.selectedItem.pk_statistic);
				tableServer.addEventListener(ResultEvent.RESULT , deleteResultHandler);
				
			}
			
		}
		
		public function deleteResultHandler(event : ResultEvent) : void {
			var lastResult : String = event.result as String;
			if(lastResult == "1") {
				param = ReportCommand.getInstance(parentName).initParam("deleteResult");
				
				var selectItem : Object = param.headGrid.selectedItem;
				var selectIndex : int = param.headGrid.selectedIndex;
				var dataProvider : ArrayCollection = param.headGrid.dataProvider as ArrayCollection;
				var newProvider : ArrayCollection = new ArrayCollection();
				
				if(dataProvider.length == 1) {
					param.headGrid.dataProvider = null;
					param.bodyGrid.dataProvider = null;
					return ;
				}
				
				
				var nextItem : Object = dataProvider.getItemAt(selectIndex == 0 ? 1 : selectIndex - 1);
				
				for each(var data : Object in dataProvider) {
					
					if(data.pk_statistic == selectItem.pk_statistic)
						continue;
					
					newProvider.addItem(data);
					
				}
				
				param.headGrid.dataProvider = newProvider;
				param.headGrid.selectedIndex = selectIndex == 0 ? 0 : selectIndex > 0 ? selectIndex - 1 : -1;
				
				if(param.headGrid.selectedIndex == -1) 
					Button(param.headCanvas.getChildByName(parentName + "_btnDelete")).enabled = false;
				
				TextInput(param.headCard.getChildByName(parentName + "_txtPeriod")).text = param.headGrid.selectedItem.period;
				TextInput(param.headCard.getChildByName(parentName + "_txtBillType")).text = param.headGrid.selectedItem.billtype;
				
				var tableServer : RemoteObject = new RemoteObject();
				tableServer.destination = IProcessor.TableProcessor;
				tableServer.queryBodyData(nextItem == null ? "" : nextItem.pk_statistic , parentName);
				tableServer.addEventListener(ResultEvent.RESULT , ReportCommand.getInstance(parentName).queryBodyData);
			} else 
				Alert.show("未知异常，删除数据失败！" , "提示" , Alert.OK|Alert.CANCEL , hrreport.ui );
			
		}
	}
}